package com.zhoug.ocr.textin.parser;

import com.google.gson.JsonArray;

import com.google.gson.JsonObject;
import com.zhoug.common.utils.CollectionUtils;
import com.zhoug.common.utils.JsonUtils;
import com.zhoug.common.utils.LogUtils;
import com.zhoug.ocr.textin.data.KeyValue;
import com.zhoug.ocr.textin.data.bill.BaseBill;
import com.zhoug.ocr.textin.data.bill.BillResult;
import com.zhoug.ocr.textin.data.bill.QuotaInvoice;
import com.zhoug.ocr.textin.data.bill.TaxiTicket;
import com.zhoug.ocr.textin.data.bill.TrainTicket;
import com.zhoug.ocr.textin.data.bill.VatInvoice;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhoug
 * @Date: 2023/3/13
 * @Description:
 */
public class BillResultParser {
    private static boolean DEBUG = true;

    private static final String TAG = ">>>BillResultParser";

    public List<BaseBill> parse(BillResult result) {
        if (DEBUG) {
            LogUtils.d(TAG, "BillResultParser parse:" + result);
        }
        if (result != null && CollectionUtils.notEmpty(result.getObjectList())) {
            List<BillResult.Object> objectList = result.getObjectList();
            List<BaseBill> bills = new ArrayList<>();
            for (BillResult.Object object : objectList) {
                if (null == object) {
                    continue;
                }
                JsonObject jsonObject = new JsonObject();
                List<KeyValue> itemList = object.getItemList();
                if (CollectionUtils.notEmpty(itemList)) {
                    for (KeyValue item : itemList) {
                        jsonObject.addProperty(item.getKey(), item.getValue());
                    }
                }
                //商品
                List<List<KeyValue>> productList = object.getProductList();
                if (CollectionUtils.notEmpty(productList)) {
                    JsonArray productArray = new JsonArray();
                    for (List<KeyValue> product : productList) {
                        if (product != null && product.size() > 0) {
                            JsonObject productJson = new JsonObject();
                            for (KeyValue item : product) {
                                productJson.addProperty(item.getKey(), item.getValue());
                            }
                            productArray.add(productJson);
                        }
                    }
                    jsonObject.add("productList", productArray);
                }
                String type = object.getType();
                String objson = jsonObject.toString();
                if (DEBUG) {
                    LogUtils.d(TAG, "parse:objson=" + objson);
                }
                BaseBill bill = null;
                if (BaseBill.TYPE_TAXI_TICKET.equalsIgnoreCase(type)) {
                    bill = JsonUtils.fromJson(objson, TaxiTicket.class);
                } else if (BaseBill.TYPE_QUOTA_INVOICE.equalsIgnoreCase(type)) {
                    bill = JsonUtils.fromJson(objson, QuotaInvoice.class);
                } else if (BaseBill.TYPE_TRAIN_TICKET.equalsIgnoreCase(type)) {
                    bill = JsonUtils.fromJson(objson, TrainTicket.class);
                } else if (BaseBill.isInvoice(type)) {
                    bill = JsonUtils.fromJson(objson, VatInvoice.class);
                }
                if (bill != null) {
                    bill.setCls(object.getCls());
                    bill.setKind(object.getKind());
                    bill.setKindDescription(object.getKindDescription());
                    bill.setType(object.getType());
                    bill.setTypeDescription(object.getTypeDescription());
                    bills.add(bill);
                }
            }
            return bills;
        }
        return null;
    }

}
